package com.shop.dao.impl;

import com.shop.dao.ShoppingDao;
import com.shop.entity.Order;
import com.shop.entity.OrderDetail;
import com.shop.entity.PayMessage;
import com.shop.exception.DAOException;
import com.shop.transaction.TransactionManager;
import com.shop.util.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ShoppingDaoImpl implements ShoppingDao {

    @Override
    public int createOrder(Order o) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "insert into orders(u_id,o_total,o_pay,o_status,o_starttime) values(?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,o.getU_id());
            ps.setDouble(2,o.getO_total());
            ps.setInt(3,o.getO_pay());
            ps.setInt(4,o.getO_status());
            ps.setString(5,o.getO_starttime());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    @Override
    public int selectId(int u_id, String time) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int o_id = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "select o_id from orders where u_id=? and o_starttime=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,u_id);
            ps.setString(2,time);
            rs = ps.executeQuery();
            if(rs.next()) {
                o_id = rs.getInt("o_id");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return o_id;
    }

    @Override
    public void createOrderDetail(OrderDetail od) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "insert into order_detail(o_id,gd_id,s_id,od_status) values(?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,od.getO_id());
            ps.setInt(2,od.getGd_id());
            ps.setInt(3,od.getS_id());
            ps.setInt(4,od.getOd_status());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
    }

    @Override
    public double getTotal(int o_id) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        double result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "select o_total from orders where o_id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,o_id);
            rs = ps.executeQuery();
            if(rs.next()) {
                result = rs.getDouble("o_total");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }

    @Override
    public int changecPayState(String o_id) throws DAOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;

        try {
            conn = TransactionManager.getTransactionConn();
            String sql = "update orders set o_pay=1,o_status=1 where o_id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(o_id));
            result = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            throw new DAOException(e);
        }finally {
            ConnectionUtils.close(rs,ps,null);
        }
        return result;
    }
}
